package com.rrd.dw.mr.gzip.kafka;

import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.third.qichacha.*;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;

/**
 * 企查查741被执行人核查
 *
 * @author xj
 */

public class QiChaChaZhixingListRecordParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 企查查741被执行人核查
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        QiChaChaZhixingListRecord obj = (QiChaChaZhixingListRecord) model;
        QiChaChaZhixingListData data = obj.getData() == null ? new QiChaChaZhixingListData() : obj.getData();

        StringBuilder sb = getBaseInfo(obj, successful);
        //数据是否存在
        sb.append(data.getVerifyResult()).append(CRT001);
        //请求状态码
        sb.append(data.getStatus()).append(CRT001);
        //是否查询成功
        sb.append(data.getMessage()).append(CRT001);
        //查询编号
        sb.append(data.getOrderNumber()).append(CRT001);
        StringBuilder detailSb = new StringBuilder(200);
        int i = 0;
        for (QiChaChaZhixingList info : data.getList()) {
            i++;
            detailSb.delete(0, detailSb.length());
            //序号
            detailSb.append(i).append(CRT001);
            //主键
            detailSb.append(info.getId() == null ? "" : info.getId()).append(CRT001);
            //案号
            detailSb.append(info.getAnno() == null ? "" : info.getAnno()).append(CRT001);
            //立案时间
            detailSb.append(info.getLiandate() == null ? "" : info.getLiandate()).append(CRT001);
            //执行法院
            detailSb.append(info.getExecuteGov() == null ? "" : info.getExecuteGov()).append(CRT001);
            //被执行人的履行情况
            detailSb.append(info.getBiaodi() == null ? "" : info.getBiaodi()).append(CRT001);
            key.set(getKey());
            val.set((sb.toString() + detailSb.toString()).replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }
        if (i == 0) {
            key.set(getKey());
            val.set((sb.toString()).replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_qichacha_zhixinglist_record_inc";
    }

    @Override
    public String getType() {
        return RecordType.QICHACHA_ZHIXINGLIST_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "企查查741被执行人核查";
    }
}
